﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class enterPwdForm : Form
    {
        private byte[] pwd;
        private bool match;
        public enterPwdForm(byte[] pss)
        {
            pwd = new byte[16];
            pwd = pss;
            InitializeComponent();
            pwdBox.Select();
            match = false;
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);                        
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            System.Security.Cryptography.MD5 md5Hasher = System.Security.Cryptography.MD5.Create();
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(pwdBox.Text));

            if (pwd.SequenceEqual(data))
            {
                match = true;
                this.Close();
            }
            else
                okButton.Text = "Wrong Password";
        }

        private void okButton_MouseLeave(object sender, EventArgs e)
        {
            okButton.Text = "OK";
        }

        private void pwdBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Return)
            {
                okButton_Click(sender, e);
            }
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (match==true) 
                this.DialogResult = DialogResult.Yes;
            else 
                this.DialogResult = DialogResult.No;
        }
    }
}
